﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS
{
    public partial class gio_hang : System.Web.UI.Page
    {
        cmsProductBL data = new cmsProductBL();
        string strFromMail = "";
        string strFromPass = "";
        string strToMail = "";
        string strDisplay = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //LoadMail();
              
                    if (Session["shop"] == null)
                    {
                        Ultility.Alert("Giỏ hàng trống!!!","/");
                    }
                    //AddItem();
                ShowData();
            }
        }



        private void ShowData()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            table.Columns.Add("Name");
            table.Columns.Add("Cost");
            table.Columns.Add("Image");
            table.Columns.Add("Quantity");
            table.Columns.Add("TotalCost");
            if (Session["shop"] != null)
            {
                foreach (var item in ((MyShopCart)Session["shop"]).ListProduct)
                {

                    DataRow row = table.NewRow();
                    row["Id"] = item.Id;
                    row["Name"] = item.Name;
                    if (item.Cost == null)
                    {
                        String.Format("{0:0,0}", 0);
                    }
                    else
                    {
                        row["Cost"] = String.Format("{0:0,0}", item.Cost);
                    }
                    row["Image"] = item.Image;
                    row["Quantity"] = item.Quantity;
                    row["TotalCost"] = String.Format("{0:0,0}", item.Quantity * item.Cost);

                    table.Rows.Add(row);
                }

                listCart.DataSource = table;
                listCart.DataBind();
            }
            else
            {
                //btnSend.Visible = false;
                //btnUpdate.Visible = false;
                //btnDel.Visible = false;
            }


            if (Session["shop"] != null)
            {
                decimal totalCost = ((MyShopCart)Session["shop"]).GetTotalCost();
                ltTotal.Text = String.Format("{0:0,0}", totalCost) + " VNĐ";

                string longCost = String.Format("{0:0}", totalCost);
                ClsDocTien cls = new ClsDocTien();
                ltStringTotal.Text = cls.DocTien(Convert.ToInt64(longCost), " đồng");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in listCart.Items)
            {
                TextBox txtQty = (TextBox)item.FindControl("txtQty");
                TextBox txtId = (TextBox)item.FindControl("txtID");
                int quantity = int.Parse(txtQty.Text);
                int id = int.Parse(txtId.Text);
                ((MyShopCart)Session["shop"]).UpdateQuantity(id, quantity);
            }

            ShowData();
        }
    }
}